//
//  CDImages.m
//  freeLance
//
//  Created by s.zalozniy on 4/13/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import "CDImages.h"
#import "CDCollections.h"
#import "CDProducts.h"
#import "Utils.h"


@implementation CDImages

@dynamic isSynchronized;
@dynamic url;
@dynamic collection;
@dynamic products;

#pragma mark - Allocators
+(CDImages *) imageWithImageInfo:(Images *)imageInfo inContext:(NSManagedObjectContext *)context {
    CDImages *newImage = [NSEntityDescription insertNewObjectForEntityForName:[[CDImages class] description]
                                                              inManagedObjectContext:context];
    [newImage updateWithImageInfo:imageInfo inContext:context];
    
    return newImage;
}


#pragma mark - Interface methods

-(void) updateWithImageInfo:(Images *)imageInfo inContext:(NSManagedObjectContext *)context {
    self.url = imageInfo.url;
    self.isSynchronized = [NSNumber numberWithBool:YES];
}


-(NSString *)imagePath {
    return [[[Utils applicationCacheDirectory] stringByAppendingPathComponent:@"images"] stringByAppendingPathComponent:[Utils md5:self.url]];
}


-(NSString *)imagePathThumbnail200 {
    return [[[Utils applicationCacheDirectory] stringByAppendingPathComponent:@"imagesThumbnail200"] stringByAppendingPathComponent:[Utils md5:self.url]];
}


-(NSString *)imagePathThumbnail60 {
    return [[[Utils applicationCacheDirectory] stringByAppendingPathComponent:@"imagesThumbnail60"] stringByAppendingPathComponent:[Utils md5:self.url]];
}

@end
